package cn.lishiyuan.algorithm.stack;

import cn.lishiyuan.algorithm.list.LinkedList;

/**
 * 使用链表结构实现的栈
 * @param <T>
 */
public class LinkedStack<T> implements LeeStack<T>{

    private final LinkedList<T> linkedList;

    public LinkedStack(){
        linkedList = new LinkedList<>();
    }

    public int count(){
        return linkedList.size();
    }

    @Override
    public boolean isEmpty() {
        return linkedList.isEmpty();
    }

    @Override
    public void push(T data) {
       linkedList.addFirst(data);
    }

    @Override
    public T pop() {
        if(linkedList.isEmpty()){
            return null;
        }
        T t = linkedList.get(0);
        linkedList.removeFirst();
        return t;
    }

    public T peek() {
        if(linkedList.isEmpty()){
            return null;
        }
        T t = linkedList.get(0);
        return t;
    }

    @Override
    public String toString() {
        return linkedList.toString();
    }
}
